package com.example.arrange;

import com.example.util.CommonUtils;

import java.util.concurrent.CompletableFuture;

public class AllOfDemo {
    public static void main(String[] args) {
        CommonUtils.printThreadLog("main start");

        CompletableFuture<String> readFileFuture1 = readFileFuture("news1.txt");
        CompletableFuture<String> readFileFuture2 = readFileFuture("news2.txt");
        CompletableFuture<String> readFileFuture3 = readFileFuture("news3.txt");

        CompletableFuture<String> resultFuture = CompletableFuture.allOf(readFileFuture1, readFileFuture2, readFileFuture3).thenApply(v -> {
            String content1 = readFileFuture1.join();
            String content2 = readFileFuture2.join();
            String content3 = readFileFuture3.join();
            return content1 + "\n" + content2 + "\n" + content3;
        });

        CommonUtils.printThreadLog("main continue");

        String result = resultFuture.join();
        System.out.println(result);

        CommonUtils.printThreadLog("main end");
    }

    private static CompletableFuture<String> readFileFuture(String path) {
        return CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("读取文件：" + path);
            return CommonUtils.readFile(path);
        });
    }

}
